{{if .TableOfContents }}
{{ $toc := .TableOfContents }}

{{/*  remove write char  */}}
{{ $toc = replaceRE `\n` ` ` $toc}}
{{ $toc = replaceRE `\s{2,}` ` ` $toc}}
{{ $toc = replaceRE `>\s<` `><` $toc}}

{{/* remove nav element */}}
{{ $toc = replaceRE `<nav.*?><ul>(.*?)</ul></nav>` `{"show":true,"items":[$1]}` $toc }}

{{/* convert ul */}}
{{ $toc = replaceRE `<ul>` `"children":{"show":true,"items":[` $toc}}
{{ $toc = replaceRE `</ul>` `]},` $toc }}

{{/* convert li */}}
{{ $toc = replaceRE `<li>` `{` $toc}}
{{/* convert a */}}
{{ $toc = replaceRE `<a href="([^<]*?)">([^<]*?)</a>` `"anchor":"$1","label":"$2",` $toc }} 
{{ $toc = replaceRE `</li>` `},` $toc}}

{{/*  remove tralling comma  */}}
{{ $toc = replaceRE `,}` `}` $toc }}
{{ $toc = replaceRE `,]` `]` $toc}}

<script>
var tocJson = '{{- $toc|safeJS -}}';
var tocObj = JSON.parse(tocJson);
</script>
{{end}}